<template>
  <div>
    <el-button type="text" size="small" @click="binaj">授权</el-button>
    <!-- 内容 -->
    <el-drawer
      style="font-size: 20px"
      title="添加角色"
      :visible.sync="dialogFormVisible"
      direction="btt"
      :before-close="handleClose"
      size="70%"
    >
      <div class="demo-drawer__content">
        <el-form-item label="角色名称:">
          <el-input
            disabled
            v-model="ids.roleName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <treeMenus :list="treeMenusData"></treeMenus>
        <div class="demo-drawer__footer">
          <el-button @click="cancelForm()">取 消</el-button>
          <el-button @click="tijiaoFrom()">提 交 </el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import login from "../api/login";
import treeMenus from "./treeMenus.vue";
import datas from "./data";
export default {
  name: "tianjiaUserpush",
  components: { treeMenus },
  props: ["ids"],
  data() {
    return {
      dialogFormVisible: false,
      treeMenusData: [],
    };
  },
  methods: {
    // 查看当前角色
    binaj() {
      this.getsa(this.ids.roleId);
    },
    // 初始化数据
    getsa(i) {
      login
        .getsa(i)
        .then((res) => {
          if (res.data.code == 20000) {
            this.extractionRecursive(res.data.list);
            this.dialogFormVisible = true;
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((error) => {
          this.dialogFormVisible = false;
          this.$message.error(error.message);
        });
      login
        .getMeun()
        .then((res) => {
          if (res.data.code == 20000) {
            this.treeMenusData = res.data.children;
            console.log(res.data);
          } else {
            this.dialogFormVisible = false;
            this.$message.error(res.data.msg);
          }
        })
        .catch((error) => {
          this.dialogFormVisible = false;
          this.$message.error(error.message);
        });
    },
    //递归提取数据
    extractionRecursive(list) {
      let tokenUser = [];
      for (let i = 0; i < list.length; i++) {
        if (list[i].menuId) {
          tokenUser.push(list[i].menuId);
        }
      }
      datas.tokenUsers(tokenUser);
    },
    // 查询脚色应有菜单
    getpathmenu(i) {
      login.getpathmenu(i).then((res) => {
        console.log(res);
      });
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(() => {
          done();
          this.cancelForm();
        })
        .catch(() => {});
    },
    cancelForm() {
      this.dialogFormVisible = false;
    },
    tijiaoFrom() {
      let data = {
        menuIds: datas.state.tokenUser,
        roleId: this.ids.roleId,
      };
      login
        .updateRoleModule(data)
        .then((res) => {
          if (res.data.code == 20000) {
            this.dialogFormVisible = false;
            this.$message({
              message: res.data.msg,
              type: "success",
            });
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch((error) => {
          this.$message.error(error.message);
        });
    },
  },
};
</script>